package com.carrie.mdg.myviewtext.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;

import com.carrie.mdg.myviewtext.R;
import com.carrie.mdg.myviewtext.fragments.AnotherRightFragment;
import com.carrie.mdg.myviewtext.fragments.LeftFragment;
import com.carrie.mdg.myviewtext.fragments.RightFragment;

import androidLib.activity.BaseActivity;

public class FragmentTestActivity extends BaseActivity {
    private Button button;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, FragmentTestActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected void initVariables() {

    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_fragment_test);
        button = (Button) findViewById(R.id.btn_set_right_fragment);
//        replaceFragment(new RightFragment());

        LeftFragment leftFragment = (LeftFragment) getSupportFragmentManager().findFragmentById(R.id.left_fragment);//可以在Activity中调用Fragment中的方法
        leftFragment.test();
    }

    @Override
    protected void initEvent() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                replaceFragment(new AnotherRightFragment());
            }
        });
    }

    @Override
    protected void loadData() {

    }

    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.right_layout, fragment);
        transaction.addToBackStack(null);//**模拟返回栈
        transaction.commit();
    }

    public void test() {

    }
}
